package com.daoeping.dao;

import com.daoeping.data.Bzn_question;
import com.daoeping.data.Bzn_questionfavgroup;
import com.daoeping.data.Bzn_questionfavorite;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.util.ConverntJsonData;
import java.util.List;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.json.JSONArray;

public class Bzn_questionfavoriteDAO extends AllDaoSupport {
	
	public Bzn_questionfavorite findBzn_questionfavoriteById(Bzn_questionfavorite bzn_questionfavorite ){
		List<Bzn_questionfavorite> bzn_questionfavoriteList =  (List<Bzn_questionfavorite>)this.selectDataByHQL("from Bzn_questionfavorite bzn where bzn.bzn_question.id = '"+bzn_questionfavorite.getBzn_question().getId()+"' and bzn.usr_user.id = '"+bzn_questionfavorite.getUsr_user().getId()+"'");
		if(bzn_questionfavoriteList.size()==1){
			return bzn_questionfavoriteList.get(0);
		}else{
			return null;
		}
		
	}
	public JsonData findAllBzn_questionfavoriteByPage(Page page,
			TableData tableData, Bzn_questionfavorite bzn_questionfavorite){
		try {
			boolean flag = false;
			StringBuilder sb1 = new StringBuilder();
			StringBuilder sb2 = new StringBuilder();
			sb1.append("select ques.id , ques.title , fav.id , user.username ,ques.createDatetime ,cate.name ,favgroup.name , favgroup.description , favgroup.id   from Bzn_questionfavorite fav left join fav.bzn_question ques left join fav.bzn_questionfavgroup favgroup left join ques.usr_user user left join ques.bsc_category cate where fav.usr_user.id = '"+bzn_questionfavorite.getUsr_user().getId()+"' ");
			sb2.append("select count(fav) from Bzn_questionfavorite fav left join fav.bzn_question ques left join fav.bzn_questionfavgroup favgroup where fav.usr_user.id = '"+bzn_questionfavorite.getUsr_user().getId()+"' ");
			if(bzn_questionfavorite.getBzn_questionfavgroup().getId()!=null&&!bzn_questionfavorite.getBzn_questionfavgroup().getId().equals("")){
				sb1.append(" and fav.bzn_questionfavgroup.id = '"+bzn_questionfavorite.getBzn_questionfavgroup().getId()+"'");
				sb2.append(" and fav.bzn_questionfavgroup.id = '"+bzn_questionfavorite.getBzn_questionfavgroup().getId()+"'");
				flag = true;
			}else{
				sb1.append(" and fav.bzn_questionfavgroup.id is null  and fav.bzn_question.id is not null ");
				sb2.append(" and fav.bzn_questionfavgroup.id is null  and fav.bzn_question.id is not null");
			}
			sb1.append(" order by fav.id desc");
			System.out.println( this.getPageList(page, sb1.toString()));
			List<Object[]> resultlist = (List<Object[]>) this.getPageList(page, sb1.toString());
			JSONArray newJson = new JSONArray();
			//empty group;group;   not group 
			if(flag&&resultlist.size()==1){
				newJson.put(0);
			}else if(flag&&resultlist.size()>1){
				newJson.put(this.getNum(page, sb2.toString())-1);
			}else{
				newJson.put(this.getNum(page, sb2.toString()));
			}
			JSONArray json = new ConverntJsonData().getJsonData(resultlist);
			JSONArray favgroupJson = new JSONArray();
			//add favorite group detail
			if(json.length()>0){
				Object[] objArray = (Object[])json.get(0);
				favgroupJson.put(objArray[6]);
				favgroupJson.put(objArray[7]);
				favgroupJson.put(objArray[8]);
			}
			newJson.put(favgroupJson);
			//remove empty record
			int length = json.length();
			if(flag&&((Object[])json.get(length-1))[0]==null){
				length = length - 1;
			}
			for (int i = 0; i < length; i++) {
				if(((Object[])json.get(i))[0]!=null){
					newJson.put(json.get(i));
				}
			}
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(newJson.toString());
			System.out.println(newJson);
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean editBzn_questionfavorite(JsonData jsonData){
		//31<del
		String [] strArr = jsonData.getJsonData().split("<");
		if(strArr[0].equals("notGroup")){
			strArr[0] = null;
		}
		String hql = "update Bzn_questionfavorite fav "  ;
		if(strArr[1].equals("del")){
			hql = hql + " set fav.bzn_questionfavgroup.id = null where fav.bzn_questionfavgroup.id = " + strArr[0] + " and fav.bzn_question.id is not null " ;
		}else{
			hql = hql + " set fav.bzn_questionfavgroup.id = "+strArr[0] +" where fav.id in ("+ strArr[1].substring(0, strArr[1].length()-1) +")";
		}
		return this.updateDataByHQL(hql);
	}
	
	public boolean delBzn_questionfavoriteByFavGroupId(Bzn_questionfavgroup bzn_questionfavgroup){
		return this.updateDataByHQL(" delete from Bzn_questionfavorite  fav where fav.bzn_questionfavgroup.id = "+bzn_questionfavgroup.getId()) ;
		
	}
	
	public  boolean delBzn_questionfavorite(JsonData jsonData){
		//123,123,
		String str = jsonData.getJsonData();
		return this.updateDataByHQL("delete from Bzn_questionfavorite fav where fav.id in ("+str.substring(0, str.length()-1)+")");
	}
	
	public boolean  delBzn_questionfavoriteByQuestionId(Bzn_question bzn_question){
		return updateDataByHQL("delete from Bzn_questionfavorite fav where fav.bzn_question.id ="+bzn_question.getId());
	}
	
	
}